package com.hxkj.controller;

import com.hxkj.client.service.CertOrgsServiceClient;
import com.hxkj.domain.enums.UserExceptionEnum;
import com.hxkj.domain.po.CertOrgs;
import com.hxkj.domain.po.Token;
import com.hxkj.service.TokenService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * created by kezhijie on 2018/10/9 16:16.
 * email to kezhijie@hxoss.com
 */
@RestController
@RequestMapping("orgs")
public class CertOrgsController {

    @Autowired
    private CertOrgsServiceClient certOrgsServiceClient;
    @Autowired
    private TokenService tokenService;

    @PostMapping("save")
    public void save(@ModelAttribute CertOrgs certOrgs, HttpServletRequest request) throws Exception {
        String userId = getUserIdByToken(request);

        certOrgs.setUserId(userId);

        ResultMessageStruct resultMessageStruct = certOrgsServiceClient.save(certOrgs);

        // 处理反馈消息
        resolveMessage(resultMessageStruct);

    }

    @PostMapping("update")
    public void update(@ModelAttribute CertOrgs certOrgs, HttpServletRequest request) throws Exception {
        String userId = getUserIdByToken(request);

        certOrgs.setUserId(userId);

        ResultMessageStruct resultMessageStruct = certOrgsServiceClient.update(certOrgs);

        // 处理反馈消息
        resolveMessage(resultMessageStruct);
    }

    @PostMapping("get")
    public CertOrgs get(HttpServletRequest request) throws Exception {
        String userId = getUserIdByToken(request);

        ResultDataStruct<CertOrgs> resultDataStruct = certOrgsServiceClient.getByUserId(userId);

        // 处理反馈消息
        return resolveDataMessage(resultDataStruct);
    }

    private String getUserIdByToken(HttpServletRequest request) {
        String accessToken = request.getHeader("accessToken");
        Token token = tokenService.validate(accessToken).getData();

        return token.getUserId();
    }

    private void resolveMessage(ResultMessageStruct resultMessageStruct) throws Exception {
        if (resultMessageStruct.getCode() == 500)
            throw new CustomMessageException(resultMessageStruct.getMessage());
        if (resultMessageStruct.getCode() != 200)
            throw new CustomException(UserExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
    }

    private <T> T resolveDataMessage(ResultDataStruct<T> resultDataStruct) throws Exception {
        if (resultDataStruct.getCode() == 500)
            throw new CustomMessageException(resultDataStruct.getMessage());
        if (resultDataStruct.getCode() != 200)
            throw new CustomException(UserExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
        return resultDataStruct.getData();
    }

}
